<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
    <el-form ref="form" :model="form" :show-message="false" label-width="100px">
      <el-form-item label="我的单位" prop="comId" required>
        <SelectCompany
          :defaultValue="form.comId"
          :disabled="!!form.id"
          :companyType="0"
          @change="(e) => changeCom('comId', e)"
          style="width: 95%"
        />
      </el-form-item>
      <el-form-item label="合作单位" prop="myComId" required>
        <SelectCompany
          :defaultValue="form.myComId"
          :disabled="!!form.id"
          :companyType="0"
          @change="(e) => changeCom('myComId', e)"
          style="width: 95%"
        />
      </el-form-item>
      <el-form-item label="合作项目" prop="checkedItems" required>
        <el-checkbox-group
          v-model="form.checkedItems"
          class="block"
          :disabled="!!form.id"
        >
          <div v-for="dict in dictRefactor" class="disFlxAC">
            <el-checkbox :label="dict" :key="dict.value">{{
              dict.label
            }}</el-checkbox>
            <el-input-number
              size="mini"
              controls-position="right"
              class="mx-10"
              style="width: 90px"
              :step="1"
              step-strictly
              v-model="dict.discount"
              placeholder="请输入折扣比例"
              :min="0"
              :max="100"
            />
            <div style="width: 100px" class="fz-12 color9">折扣比例(%)</div>
          </div>
        </el-checkbox-group>
        <div class="colorRED fz-12">
          折扣比例默认为100%,100%则是保持原价不进行折扣;
        </div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="btnLoading" @click="submitForm"
        >确 定</el-button
      >
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import SelectCompany from "@/components/SelectCompany";
import { addTeam, updateTeams } from "@/api/cooperation/cooperation";
export default {
  dicts: ["cooperation_projects"],
  components: {
    SelectCompany,
  },
  props: {
    title: {
      type: String,
      default: "添加单位合作关系",
    },
  },
  data() {
    return {
      open: false,
      form: {
        id: undefined,
        comId: "",
        myComId: "",
        checkedItems: [],
      },
      dictRefactor: [],
      btnLoading: false,
    };
  },
  methods: {
    show(row) {
      Object.assign(this.$data.form, this.$options.data().form);
      this.open = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      });
      if (row && row.id) {
        this.form.id = row.id;
        this.form.comId = row.comId;
        this.form.myComId = row.myComId;

        this.dictRefactor = this.dict.type.cooperation_projects.filter((v) => {
          if (v.value === row.apType) {
            this.$set(v, "discount", row.discount);
            this.form.checkedItems = [v];
            return v;
          }
        });
      } else {
        this.dictRefactor = this.dict.type.cooperation_projects.map((v) => {
          this.$set(v, "discount", 100);
          return v;
        });
        this.form.checkedItems = this.dictRefactor;
      }
    },
    changeCom(key, value) {
      this.form[key] = value;
      if (this.form.comId === this.form.myComId) {
        this.$message.error("不能选择相同单位作为合作单位");
        this.$nextTick(() => {
          this.form[key] = "";
          this.$refs.form.validateField(key);
        });
      }
    },
    cancel() {
      this.open = false;
    },
    submitForm() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          this.btnLoading = true;
          try {
            for (let i = 0; i < this.form.checkedItems.length; i++) {
              const item = this.form.checkedItems[i];
              const params = {
                id: this.form.id,
                comId: this.form.comId,
                myComId: this.form.myComId,
                apType: item.value,
                discount: item.discount,
              };
              if (params.id) {
                await updateTeams(params);
              } else {
                params.apStatus = "1"; // 是否需要审核, 1则不需要
                await addTeam(params);
              }
            }
            this.btnLoading = false;
            this.open = false;
            this.$emit("refresh");
          } catch (error) {
            this.btnLoading = false;
          }
        }
      });
    },
  },
};
</script>
